<template>
  <!-- 内部领用 -->
  <div>
    <FilterTable
      ref="table"
      columnsRef="reportInternalsjjie"
      :columns="columns"
      selectionType="single"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.selectColumns = arr)"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件综合情况查询-内部领用.xlsx"
      :dataSource="dataSource"
      :isMemoryPagination="true"
    >
      <!-- :fetchapi="fetchApi"
    :params="fetchParams"
      :datakey="'rows'"-->
      <template slot="controls">
        <AjaxButton size="small" type="primary" :disabled="printFlag" icon="el-icon-printer" :click="printHandle">打印</AjaxButton>
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>
    <!-- 打印 -->
    <div v-if="showPrint">
      <BasePrint ref="print" :data="printList" template="templateInternalReceive" />
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInOutStockReport, getAllUser, exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { printData } from '@/api/part/partOutbound/internalReceive/index';
import { dealerPrintApi } from '@/api/repair/index.js';
import { exportExcelFile } from '@/utils/index';
import { getOrgList } from '@/api/repair/repairBusiness/instituteDefined';
export default {
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [], // 选中行数据
      showPrint: false, // 打印显示
      printParams: {},
      printList: {}, // 打印数据
      printFlag: false, // 打印按钮是否禁用
      departmentList: [], // 部门列表
      dataSource: []
    };
  },
  watch: {
    fetchParamObj(fetchParamObj) {
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    this.getStorage();
    this.getDepartmentList();
    this.getEmployee();
    // this.getPrintParams();
  },
  methods: {
    // 导出翻译
    exportFun(row) {
      for (const key in row) {
        if (row.hasOwnProperty(key)) {
          let element = row[key];
          switch (key) {
            case 'OUT_PRICE':
              break;
            case 'COST_AMOUNT':
              row[key] = Number(element).toFixed(2);
              break;
            case 'COST_PRICE':
              row[key] = Number(element).toFixed(2);
              break;
            case 'OUT_AMOUNT':
              row[key] = Number(element).toFixed(2);
              break;
            // case 'OUT_QUANTITY': row[key] = Number(element).toFixed(2);
            // break;
            default:
              break;
          }
        }
      }
      if (row.RECEIPTOR) {
        row.RECEIPTOR = this.userList[row.RECEIPTOR];
      }
    },
    async getPrintParams() {
      let params = { vModule: '90211009', vNormalCode: 'BJNL' };
      const res = await dealerPrintApi(params);
      if ((res.resultCode == 200) & res.data.length) {
        this.printParams = res.data[0];
      }

      console.log('打印的参数', this.printParams);
    },
    // 获取部门列表
    async getDepartmentList() {
      // console.log(this.userList);
      // let res = (await getOrgList()).data; // getOrgsTree
      // if (res.data != null) {
      //   this.departmentList = res.data.map(x => ({ text: x.text, value: x.id + '' }));
      // }
    },
    // 打印数据接口
    async getPrintData() {
      // console.log(this.selectColumns[0].PART_ALLOCATE_IN_ID);
      const res = await printData(this.selectColumns[0].PART_INNER_ID); // 获取当前行数据
      this.printList = res.data;
    },
    // 打印处理
    async printHandle() {
      // this.getPrintParams();
      // 判断是否选中数据
      if (this.selectColumns.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      await this.getPrintData();
      this.showPrint = true;
      console.log(this.printList);
      this.printList.baseInfo.USER_NAME = this.userList[this.printList.baseInfo.USER_ID];
      this.printList.baseInfo.RECEIPTOR_USER = this.userList[this.printList.baseInfo.RECEIPTOR];
      // todo element.baseInfo.RECEIPTOR_NAME = departmentList[element.baseInfo.ORG_CODE];
      // 打印
      this.$nextTick(() => {
        // console.log(123);

        this.$refs.print.EXCUTE_PRINT();
      });
    },
    // 翻译名字
    getUserName(val) {
      return this.userList[val];
    },
    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      // this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      this.fetchParams = Object.assign({}, fetchParamObj);
      try {
        this.$refs.table.START_LOADING();
        this.dataSource = [];
        let res = await queryPartInOutStockReport(this.fetchParams);
        this.dataSource = res.data;
        this.$refs.table.STOP_LOADING();
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'内部领用')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').dictItems = this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({
        text: x.STORAGE_NAME,
        value: x.STORAGE_CODE + ''
      }));
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        { title: ' 出库单编号', dataIndex: 'RECEIPT_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 原单编号', dataIndex: 'fake1', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 出库方式', dataIndex: 'fake2', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: ' 出库日期', dataIndex: 'FINISHED_DATE', sorter: true, filter: true, filterType: 'date-range', showOverflowTooltip: true, exportFormat: { t: 'd' } },
        // { title: ' 客户名称', dataIndex: 'fake3', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 牌照号', dataIndex: 'fake4', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: ' 仓库代码', dataIndex: 'STORAGE_CODE', dictItems: [], editItems: [], sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 税率(%)', dataIndex: 'fake5', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: ' 操作员代码', dataIndex: 'OPERETOR', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        {
          title: ' 操作员名称',
          dataIndex: 'OPERETOR_NAME',
          render: props => {
            return <span>{this.userList[props.row.OPERETOR]}</span>;
          },
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '部门名称',
          dataIndex: 'DEPT_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '申请人',
          dataIndex: 'RECEIPTOR',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.getUserName(props.row.RECEIPTOR)}</span>;
          },
          showOverflowTooltip: true
        },
        { title: ' 配件代码', dataIndex: 'PART_NO', width: '160', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 批号', dataIndex: 'fake7', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: ' 配件名称', dataIndex: 'PART_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // {
        //   title: '配件说明',
        //   dataIndex: 'PART_DESCRIPTION',
        //   sorter: true,
        //   showOverflowTooltip: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // { title: ' CCC件类型', dataIndex: 'IS_CCC_PART', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 保质期', dataIndex: 'SHELF_LIFT_MARK' },
        // {
        //   title: ' ABC分类',
        //   dataIndex: 'ABC_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('8092'),
        //   dictItems: this.createDictList('8092'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
        //   },
        //   showOverflowTooltip: true
        // },
        // { title: ' 计量单位', dataIndex: 'UNIT_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: ' 出库数量', dataIndex: 'OUT_QUANTITY', summation: true, sorter: true, filter: true, filterType: 'number', showOverflowTooltip: true },
        { title: ' 含税出库单价', dataIndex: 'OUT_PRICE', sorter: true, filter: true, filterType: 'number', precision: 2, summation: false, showOverflowTooltip: true },
        { title: ' 含税成本单价', dataIndex: 'COST_PRICE', sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } },
        { title: ' 含税出库金额', dataIndex: 'OUT_AMOUNT', summation: true, sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } },
        { title: ' 含税成本金额', dataIndex: 'COST_AMOUNT', summation: true, sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } }
        // { title: ' 主组号', dataIndex: 'MAIN_GROUP_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 销售方式', dataIndex: 'fake8', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: ' 发布单号', dataIndex: 'fake9', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // {
        //   title: '备注',
        //   dataIndex: 'REMARK',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // }

        // {
        //   title: '对应单编号',
        //   dataIndex: 'RECEIPT_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '牌照号',
        //   dataIndex: 'license',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '出库日期',
        //   width: 140,
        //   dataIndex: 'FINISHED_DATE',
        //   dateFormat: 'yyyy-MM-dd hh:mm:ss',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'date-range'
        // },
        // {
        //   title: '仓库代码',
        //   dataIndex: 'STORAGE_CODE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '仓库名称',
        //   dataIndex: 'STORAGE_NAME',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '操作员名称',
        //   dataIndex: 'OPERETOR',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },

        // {
        //   title: '出库方式',
        //   dataIndex: 'outType',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '配件代码',
        //   dataIndex: 'PART_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '配件名称',
        //   dataIndex: 'PART_NAME',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '出库数量',
        //   dataIndex: 'OUT_QUANTITY',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number',
        //   precision: 2,
        //   summation: true
        // },
        // {
        //   title: '含税出库单价',
        //   dataIndex: 'OUT_PRICE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number'
        // },
        // {
        //   title: '不含税成本单价',
        //   dataIndex: 'COST_PRICE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number'
        // },
        // {
        //   title: '含税出库金额',
        //   dataIndex: 'OUT_AMOUNT',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number',
        //   precision: 2,
        //   summation: true
        // },
        // {
        //   title: '不含税成本金额',
        //   dataIndex: 'COST_AMOUNT',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number',
        //   precision: 2,
        //   summation: true
        // },
        // {
        //   title: '销售方式',
        //   dataIndex: 'SALE_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: 'CCC件类型',
        //   dataIndex: 'IS_CCC_PART',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // }
      ];
    },
    // 操作员名称
    async getEmployee() {
      // 获取人员列表
      const res = await getAllUser();
      if (res.resultCode == 200) {
        this.columns.find(item => item.dataIndex === 'OPERETOR_NAME').dictItems = res.data.map(x => ({
          text: x.employeeName,
          value: x.userId + ''
        }));
      }
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
